<template>
  <div class="app-container">
    <el-form label-width="220px">
      <div><i class="square"></i>更新信息</div>
      <el-form-item label="标题：" required>
        <el-input :maxlength=20 v-model='postForm.title' clearable placeholder='请输入标题限20个字符' style='width: 250px;'></el-input>
      </el-form-item>
      <el-form-item label="详情描述：">
        <tinymce ref="tinymce" :height="300" v-model="postForm.content" style="width: 550px;" :key="1"></tinymce>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitBack">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { toGetTheUpdate, saveUpdate } from '@/api/extra'
import Tinymce from '@/components/Tinymce'
export default {
  components: {
    Tinymce
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  created() {
    if (this.isEdit) {
      this.getTheUpdate()
    }
  },
  data() {
    return {
      postForm: {
        uiId: 0,
        title: '',
        content: ''
      }
    }
  },
  methods: {
    getTheUpdate() {
      toGetTheUpdate(this.$route.params.upId).then(res => {
        this.postForm = res.data.data
      })
        .catch(err => {
          console.log(err)
        })
    },
    submitBack() {
      if (!this.postForm.title || this.postForm.title.trim() == '') {
        this.$message({
          type: 'warning',
          message: ' 标题必填! '
        })
        return
      }
      saveUpdate(this.postForm)
        .then(res => {
          this.$message({
            type: 'success',
            message: ' 保存成功! '
          })
          this.close()
        })
        .catch(err => {
          console.log(err)
        })
    },
    close() {
      this.$store.dispatch('delVisitedViews', this.$route).then((views) => {
        const latestView = views.slice(-1)[0]
        console.log(latestView)
        if (latestView) {
          this.$router.push(latestView.path)
        } else {
          this.$router.push('/')
        }
      })
    },
    cancel() {

    }
  }
}
</script>
<style>

</style>
